import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  HttpCode,
  HttpStatus,
  Ip,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { emailCode } from './dto/email-auth.dto';
import { judgeEmailDto } from './dto/judge.auth.dto';
import { CodeService } from 'src/code/code.service';
import { Auth } from 'src/jwt/jwt.decorator';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiResponse,
} from '@nestjs/swagger';
import {
  CreaseResponse,
  EmailCode,
  responseLogin,
  responseUser,
  responseValidateemailCode,
} from './entities/Response';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly captcha: CodeService,
  ) {}
  //注册账号
  @Post('register')
  @HttpCode(200)
  @ApiOperation({ summary: '注册接口' })
  @ApiBody({ type: CreateAuthDto })
  @ApiResponse({
    status: 200,
    description: '恭喜您注册成功',
    type: CreaseResponse,
  })
  async create(@Body() createAuthDto: CreateAuthDto): Promise<CreaseResponse> {
    const result = await this.authService.create(createAuthDto);
    return { data: { token: result.token } };
  }

  //发送邮箱验证码
  @HttpCode(200)
  @Post('emailCode')
  @ApiOperation({ summary: '发送邮箱验证码' })
  @ApiBody({ type: emailCode })
  @ApiResponse({
    status: 200,
    description: '恭喜您发送验证码成功',
    type: EmailCode,
  })
  async setEmailCode(
    @Body() email: emailCode,
    @Ip() ip: any,
  ): Promise<EmailCode> {
    const result = await this.authService.setEmailCode(email, ip);
    return { data: { message: result.message, key: result.key } };
  }
  //验证邮箱验证码是否正确
  @Post('validateEmailCode')
  @ApiOperation({ summary: '验证邮箱验证码' })
  @ApiBody({ type: judgeEmailDto })
  @ApiResponse({
    status: 200,
    description: '邮箱验证码验证成功',
    type: responseValidateemailCode,
  })
  @HttpCode(200)
  async findOne(
    @Body() dto: judgeEmailDto,
  ): Promise<responseValidateemailCode> {
    const result = await this.authService.findOne(dto);
    return { data: { message: result.message } };
  }

  //登录接口
  @Post('login')
  @ApiOperation({ summary: '登录接口' })
  @ApiBody({ type: UpdateAuthDto })
  @ApiResponse({
    status: 200,
    description: '邮箱验证码验证成功',
    type: responseLogin,
  })
  @HttpCode(200)
  async login(@Body() dto: UpdateAuthDto) {
    //判断邮箱登录时的验证码是否正确
    // await this.captcha.findOne(dto.captcha as any);
    const result = await this.authService.login(dto);
    return {
      data: {
        id: result.id,
        userName: result.userName,
        avatar: result.avatar,
        name: result.name,
        gender: result.gender,
        createAt: result.createAt,
        updateAt: result.updateAt,
        token: result.token,
      },
    };
  }
  //注销用户
  @Delete('deleteuser/:id')
  @Auth()
  @ApiOperation({ summary: '注销用户接口' })
  @ApiParam({ name: 'id', description: '请输入您要注销的id用户' })
  //添加token
  @ApiBearerAuth()
  @ApiResponse({
    status: 200,
    description: '邮箱验证码验证成功',
    type: responseUser,
  })
  async deleteUser(@Param('id') id: string): Promise<responseUser> {
    const result = await this.authService.remove(+id);
    return { data: { message: result.message } };
  }
}
